package com.lv.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lv.pojo.Grade;
import com.lv.service.GradeService;
import com.lv.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:晓风残月Lx
 * @Date: 2022/4/28 19:05
 */

@Api(tags = "年级控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {

    @Autowired
    private GradeService gradeService;


    @ApiOperation("获取所有Grade信息")
    @GetMapping("/getGrades")
    public Result getGrades(){
        List<Grade> grades=gradeService.getGrades();
        return Result.ok(grades);
    }


    @ApiOperation("添加和修改Grade信息,有id为修改，没有则是增加")
    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdateGrade(@ApiParam("JSON的Grade对象") @RequestBody Grade grade){
        // 调用服务层方法，实现添加或者修改成绩信息
        gradeService.saveOrUpdate(grade);
        return Result.ok();
    }


    @ApiOperation("根据年级名称模糊查询，带分页")
    @GetMapping("/getGrades/{pageNo}/{pageSize}")
    public Result getGrades(@ApiParam("分页查询的页码数") @PathVariable("pageNo") Integer pageNo,
                            @ApiParam("分页查询的页大小") @PathVariable("pageSize") Integer pageSize,
                            @ApiParam("分页查询模糊匹配的名称") String gradeName){

       // 分页 带条件查询
        Page<Grade> page=new Page<>(pageNo,pageSize);
        //通过服务层
        IPage<Grade> pageRs=gradeService.getGradeByOpr(page,gradeName);
        // 封装Result对象并返回
        return Result.ok(pageRs);
    }


    @ApiOperation("删除Grade信息")
    @DeleteMapping("/deleteGrade")
    public Result deleteGradeById(@ApiParam("要删除的所有的grade的id的JSON集合") @RequestBody List<Integer> ids){
        gradeService.removeByIds(ids);
        return Result.ok();
    }

}
